package com.kjlink.internal.workreport.report.service;

import cn.xuqiudong.common.base.lookup.Lookup;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.service.BaseService;
import cn.xuqiudong.common.util.poi.export.ExportExcel;
import com.kjlink.internal.workreport.base.constant.CommonConstant;
import com.kjlink.internal.workreport.report.lookup.WorkReportLookup;
import com.kjlink.internal.workreport.report.mapper.WorkReportMapper;
import com.kjlink.internal.workreport.report.model.WorkReport;
import com.kjlink.internal.workreport.report.vo.DayReportVo;
import com.kjlink.internal.workreport.stats.lookup.UserWeekLookup;
import com.kjlink.internal.workreport.stats.vo.UserReportTimeVo;
import com.kjlink.internal.workreport.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能: : Service
 *
 * @author Vic.xu
 * @since 2024-04-29 13:37
 */
@Service
public class WorkReportService extends BaseService<WorkReportMapper, WorkReport> {

    @Resource
    private UserService userService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    @Override
    public PageInfo<WorkReport> page(Lookup lookup) {
        startPage(lookup.getPage(), lookup.getSize());
        List<WorkReport> dataList = mapper.list(lookup);
        return PageInfo.instance(dataList, lookup);
    }

    public DayReportVo getDayReport(Integer id) {
        List<WorkReport> workReports = sameDayReport(id);
        return convert(workReports);
    }


    public List<WorkReport> sameDayReport(Integer id) {
        return mapper.sameDayReport(id);
    }

    public DayReportVo getDayReportByDate(Integer userId, LocalDate date) {
        List<WorkReport> workReports = mapper.getDayReportByDate(userId, date);
        DayReportVo vo = new DayReportVo();
        vo.setReports(workReports);
        vo.setDay(date);
        vo.setUser(userService.findById(userId));
        return vo;
    }

    private DayReportVo convert(List<WorkReport> workReports) {
        if (workReports == null || workReports.isEmpty()) {
            return null;
        }
        DayReportVo vo = new DayReportVo();
        vo.setReports(workReports);
        WorkReport workReport = workReports.get(0);
        vo.setDay(workReport.getDate());
        vo.setUser(userService.findById(workReport.getUserId()));
        return vo;
    }


    public List<WorkReport> findWorkReportByJira(String jira) {
        if (jira == null) {
            return new ArrayList<>();
        }
        WorkReportLookup lookup = new WorkReportLookup();
        lookup.setJira(jira);
        return mapper.list(lookup);
    }

    public List<WorkReport> findWorkReportByParentJira(String parentJira) {
        if (parentJira == null) {
            return new ArrayList<>();
        }
        WorkReportLookup lookup = new WorkReportLookup();
        lookup.setParentJira(parentJira);
        return mapper.list(lookup);
    }

    /**
     * 获取项目的总工时
     *
     * @param projectId 项目id
     * @return 工时
     */
    public Integer totalTime(Integer projectId) {
        return mapper.totalTime(projectId);
    }

    /**
     * 获取项目某年的总工时
     *
     * @param projectId 项目id
     * @param year      年份
     * @return 工时
     */
    public Integer yearTime(Integer projectId, int year) {
        return mapper.yearTime(projectId, year);
    }

    /**
     * 获取项目某月的总工时
     *
     * @param projectId 项目id
     * @param year      年份
     * @param month     月份
     * @return 工时
     */
    public Integer monthTime(Integer projectId, int year, int month) {
        return mapper.monthTime(projectId, year, month);
    }


    public void export(HttpServletResponse response, WorkReportLookup lookup) throws IOException {
        lookup.setSize(CommonConstant.MAX_EXPORT_SIZE);
        PageInfo<WorkReport> page = this.page(lookup);
        List<WorkReport> list = page.getDatas();
        new ExportExcel("日报列表", WorkReport.class).setDataList(list)
                .write(response, System.currentTimeMillis() + "日报列表.xlsx").dispose();


    }

    /**
     * 员工周工时统计
     */
    public List<UserReportTimeVo> weekTimeStats(UserWeekLookup lookup) {
        return mapper.weekTimeStats(lookup);
    }

    /**
     * 当前项目是否存在日报
     *
     * @param projectId
     */
    public boolean existsWorkReport(int projectId) {
        int num = mapper.totalWorkReport(projectId);
        return num > 0;
    }
}
